package main

import (
	"fmt"
	"time"

	"github.com/spf13/viper"
)

var (
	cookie      string
	pagelimit   int
	keywordlist []string
	filename    string
	todolist    map[string]string //key is name, value is uid
	debug       bool
)

func init() {
	// viper.SetConfigName("config")
	// viper := viper.New()

	viper.SetConfigName("config") // name of config file (without extension)
	viper.SetConfigType("yaml")   // REQUIRED if the config file does not have the extension in the name
	// viper.AddConfigPath("/etc/appname/")  // path to look for the config file in
	// viper.AddConfigPath("$HOME/.appname") // call multiple times to add many search paths
	viper.AddConfigPath(".")    // optionally look for config in the working directory
	err := viper.ReadInConfig() // Find and read the config file
	if err != nil {             // Handle errors reading the config file
		panic(fmt.Errorf("fatal error config file: %w", err))
	}

	// viper.AddConfigPath("./config.yaml")
	// viper.SetConfigType("yaml")
	// if err := viper.ReadInConfig(); err == nil {
	// 	if v, ok := err.(viper.ConfigFileNotFoundError); ok {
	// 		panic("the config file is not founded!")
	// 	} else {
	// 		panic("config error: " + v.Error())
	// 	}
	// }

	cookie = viper.GetString("cookie")
	// uidlist = viper.GetStringSlice("uid")
	// namelist = viper.GetStringSlice("name")

	todolist = viper.GetStringMapString("todolist")
	keywordlist = viper.GetStringSlice("keyword")

	viper.SetDefault("filename", time.Now().Format("01-02-15-04-05"))
	viper.SetDefault("debug", false) //默认关闭debug
	viper.SetDefault("pagelimit", 1) //默认1页

	debug = viper.GetBool("debug")
	pagelimit = viper.GetInt("pagelimit")
	filename = viper.GetString("filename")
}
